Provide ability to write as text instead of as Blob #35
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently,
write()
writes everything as aBlob
. On iOS and Android, cordova-plugin-file convertsBlob
s toArrayBuffer
s, which the iOS/Android platforms then convert to base64 for transfer to the native layer. (Windows supports marshalingBlob
s directly from JS to native, so that platform skips theArrayBuffer
and base64 conversions.)I haven't profiled Android in as much detail, but on iOS that conversion to base64 is slow and memory intensive. (Some rough timings below.) An easy workaround for text files is to skip the conversion to
Blob
and write as text. That satisfies our use case of writing large JSON files.What is the intention behind using
Blob
s? This PR currently comments out theBlob
conversion completely. I'm assuming that breaks other use cases, so I'm open to amending the PR. For our purposes, we just need a way to opt out of theBlob
conversion. An optional flag towrite()
would be great. I could also imagine a CordovaPromiseFS constructor option, or maybe the behavior should be platform dependent. I figured I'd start with the simple approach before reaching out about API design.write()
timings:OS: iOS 11.3
Device: iPad Model A 1489 (iPad Mini 2nd Gen)
Cordova iOS platform: 4.4.0
cordova-plugin-file version: 4.3.3
cordova-promise-fs version: 1.2.5
Test - Write a 3.0MB JSON file. Use
console.time
to measure time from callingwrite()
until Promise is resolved.with
Blob
conversion - 3304 ms (median of 18 runs)writing as text - 575 ms (median of 19 runs)
Let me know if I can provide more info.